package serviceimpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import domain.GrashBean;
import domain.Tax;
import lib.Execute;
import lib.ExecuteLocal;
import lib.MySQLHelper;
import lib.MySQLLoaclHelper;
public class GraspBase {

	private static String post_sql = "insert into wp_posts (post_author,post_date,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_name,to_ping,pinged,post_content_filtered,post_type) value(?,?,?,?,?,?,?,?,?,?,?,?,?)";
	private static String postmeta_sql = "insert into wp_postmeta (post_id,meta_key,meta_value) value(?,?,?)";
	private static String term_sql = "insert into wp_terms (name,slug) value(?,?)";
	private static String term_relationships_sql = "insert into wp_term_relationships (object_id,term_taxonomy_id) value(?,?)";
	private static String term_taxonomy_sql = "insert into wp_term_taxonomy (term_id,description,taxonomy,count) value(?,?,?,1) on duplicate key  update count = count + 1";
	private static String taxonomy_sql = "select * from wp_term_taxonomy where term_id = ? and taxonomy = ?";
	public void insertDb(GrashBean grashBean,int isPost) throws Exception {
		Date date =  new Date();
		MySQLLoaclHelper.withTransaction(new ExecuteLocal() {
			@Override
			public void call() throws Exception {
				
					System.out.println("............"+grashBean.post_title);
					long post_id = executeUpdateResponse(post_sql, grashBean.post_author, grashBean.post_date,
							grashBean.post_content, grashBean.post_title, "",
							grashBean.post_status, grashBean.comment_status, grashBean.ping_status, grashBean.post_name, "",
							"", "", grashBean.post_type);

					List<Object[]> relationships = new ArrayList<>();
					System.out.println("...Taxsize:"+grashBean.taxs.size());
					for (Tax tax : grashBean.taxs) {
						List<Map> taxMap = MySQLLoaclHelper.find("select * from wp_terms where name = ?", tax.name);
						if (taxMap.size() == 0) {
							tax.termId = executeUpdateResponse(term_sql, new Object[] { tax.name, tax.sulg });
						} else {
							for (Map map : taxMap) {
								List<Map> taxonomyMap = MySQLLoaclHelper.find(taxonomy_sql, map.get("term_id"),tax.taxonomy);
								if(taxonomyMap.size()==0) {
									tax.termId = executeUpdateResponse(term_sql, new Object[] { tax.name, tax.sulg });
								}else {
									tax.termId = Long.valueOf(taxonomyMap.get(0).get("term_id").toString());
								}
							}
							
						}
						//更新数量
						long term_taxonomy_id = executeUpdateResponse(term_taxonomy_sql, new Object[] { tax.termId, "", tax.taxonomy });
						System.out.println(term_taxonomy_id+"..."+tax.termId+"..."+tax.name+"..."+tax.taxonomy);
						relationships.add(new Object[] { post_id, term_taxonomy_id });
					}
					// 关联term和文章表
					executeBatch(term_relationships_sql, relationships);
					// post meta信息
					List<Object[]> paramsList = new ArrayList<>();
					paramsList.add(new Object[] { post_id, "_edit_lock", date.getTime() + ":1" });
					paramsList.add(new Object[] { post_id, "_edit_last", 1 });
					paramsList.add(new Object[] { post_id, "views", 0 });
					paramsList.add(new Object[] { post_id, "filmname", grashBean.filmname });
					paramsList.add(new Object[] { post_id, "introduction", grashBean.introduction });
					paramsList.add(new Object[] { post_id, "keywords", grashBean.keywords });
					paramsList.add(new Object[] { post_id, "clarity", grashBean.clarity });
					paramsList.add(new Object[] { post_id, "screenwriter", grashBean.screenwriter });
					paramsList.add(new Object[] { post_id, "ontime", grashBean.ontime });
					paramsList.add(new Object[] { post_id, "button2", grashBean.button2 });
					paramsList.add(new Object[] { post_id, "url2", grashBean.url2 });
					paramsList.add(new Object[] { post_id, "wangpan", grashBean.wangpan });
					paramsList.add(new Object[] { post_id, "zm_like", 0 });
					executeBatch(postmeta_sql, paramsList);
				//正式库
				if(isPost == 1) {
					MySQLHelper.withTransaction(new Execute() {
						@Override
						public void call() throws Exception {
							
							long post_id = executeUpdateResponse(post_sql, grashBean.post_author, grashBean.post_date,
									grashBean.post_content, grashBean.post_title, "",
									grashBean.post_status, grashBean.comment_status, grashBean.ping_status, grashBean.post_name, "",
									"", "", grashBean.post_type);

							List<Object[]> relationships = new ArrayList<>();
							for (Tax tax : grashBean.taxs) {
								List<Map> taxMap = MySQLLoaclHelper.find("select * from wp_terms where name = ?", tax.name);
								if (taxMap.size() == 0) {
									tax.termId = executeUpdateResponse(term_sql, new Object[] { tax.name, tax.sulg });
								} else {
									for (Map map : taxMap) {
										List<Map> taxonomyMap = MySQLLoaclHelper.find(taxonomy_sql, map.get("term_id"),tax.taxonomy);
										if(taxonomyMap.size()==0) {
											tax.termId = executeUpdateResponse(term_sql, new Object[] { tax.name, tax.sulg });
										}else {
											tax.termId = Long.valueOf(taxonomyMap.get(0).get("term_id").toString());
										}
									}
									
								}
								//更新数量
								long term_taxonomy_id = executeUpdateResponse(term_taxonomy_sql, new Object[] { tax.termId, "", tax.taxonomy });
								System.out.println(term_taxonomy_id+"..."+tax.termId+"..."+tax.name+"..."+tax.taxonomy);
								relationships.add(new Object[] { post_id, term_taxonomy_id });
							}
							// 关联term和文章表
							executeBatch(term_relationships_sql, relationships);
							// post meta信息
							List<Object[]> paramsList = new ArrayList<>();
							paramsList.add(new Object[] { post_id, "_edit_lock", date.getTime() + ":1" });
							paramsList.add(new Object[] { post_id, "_edit_last", 1 });
							paramsList.add(new Object[] { post_id, "views", 0 });
							paramsList.add(new Object[] { post_id, "filmname", grashBean.filmname });
							paramsList.add(new Object[] { post_id, "introduction", grashBean.introduction });
							paramsList.add(new Object[] { post_id, "keywords", grashBean.keywords });
							paramsList.add(new Object[] { post_id, "clarity", grashBean.clarity });
							paramsList.add(new Object[] { post_id, "screenwriter", grashBean.screenwriter });
							paramsList.add(new Object[] { post_id, "ontime", grashBean.ontime });
							paramsList.add(new Object[] { post_id, "button2", grashBean.button2 });
							paramsList.add(new Object[] { post_id, "url2", grashBean.url2 });
							paramsList.add(new Object[] { post_id, "wangpan", grashBean.wangpan });
							paramsList.add(new Object[] { post_id, "zm_like", 0 });
							executeBatch(postmeta_sql, paramsList);
							}
					});
				}
			}
		});

	}
}
